package com.central.apps.handler.timeTask;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.quartz.QuartzJob;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.schedule.JobParams;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 档案模板任务
 *
 * @author JHY
 * @date 2023/02/10
 */
@Component
@Slf4j
//禁止并发执行(Quartz不要并发地执行同一个job定义（这里指一个job类的多个实例）)
@DisallowConcurrentExecution
public class ArchiveTemplateTask extends QuartzJob {
    @Autowired
    private HandlerService handlerService;

    @Override
    protected void executeJob(JobExecutionContext context, String jobnum, String jobname, List<JobParams> jobparams) throws JobExecutionException {
        System.out.println("=====> 批量备份归档文件务开始 <=====");
        try {
            List<Map<String, Object>> archivesearchList = handlerService.findBySql("SELECT * FROM `gq_archivesearch` WHERE TIMESTAMPDIFF(DAY, createtime,NOW()) > 30");
            for (Map<String, Object> item : archivesearchList) {
                Long id = MapUtil.getLong(item, "id");
                item.put("backtime", new Date());
                handlerService.insert("gq_archivesearch_back", item);
                handlerService.delete("gq_archivesearch", id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("=====> 批量备份归档文件务结束 <=====");
    }
}
